//
//  LykDaoHangView.m
//  GiftSay
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "LykDaoHangView.h"
#import "HomeDaoHangCollectionViewCell.h"
#import "DownView.h"
@interface LykDaoHangView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UIView *daoHangView;
@property (nonatomic, strong) UICollectionView *collectionview;
@property (nonatomic, assign) NSInteger indexpathItem;
@property (nonatomic, assign) CGFloat heightOld;
@property (nonatomic, strong) NSArray *showArray;
//按钮
@property (nonatomic, strong) UIButton *view;
//下拉页面
@property (nonatomic, strong) DownView *downView;
@property (nonatomic, strong) UIButton *viewUp;
@property (nonatomic, assign) BOOL ifClick;
@property (nonatomic, strong) UIButton *done;
@end

@implementation LykDaoHangView
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = NO;
        self.heightOld = self.height;
    }
    return self;
}
//数据直通车
-(void)setDaoHangArray:(NSArray *)daoHangArray {
    _daoHangArray = daoHangArray;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOpen" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOff" object:nil];
    NSArray* array = [NSArray arrayWithArray:[self getArray]];
    if (array.count > 0) {
        self.showArray = array;
    } else {
        self.showArray = self.daoHangArray;
    }
    
    self.daoHangView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width - self.height, self.height)];
    [self addSubview:self.daoHangView];
//    下拉按钮
    self.view = [UIButton buttonWithType:UIButtonTypeSystem];
    self.view.frame = CGRectMake(self.width - self.height, 0, self.height, self.height);
    [self.view setImage:[[UIImage imageNamed:@"down"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.view addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:self.view];
    
//    下拉页面
    self.downView = [[DownView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0)];
    self.downView.alpha = 0;
    self.downView.dataArray = self.daoHangArray;
    [self addSubview:self.downView];
    

    
//    collection
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(self.daoHangView.width / 5, self.daoHangView.height);
    
    self.collectionview = [[UICollectionView alloc] initWithFrame:self.daoHangView.bounds collectionViewLayout:layout];
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.pagingEnabled = NO;
    self.collectionview.bounces = NO;
    self.collectionview.backgroundColor = [UIColor whiteColor];
    self.collectionview.showsVerticalScrollIndicator = FALSE;
    self.collectionview.showsHorizontalScrollIndicator = FALSE;
    [self.collectionview registerClass:[HomeDaoHangCollectionViewCell class] forCellWithReuseIdentifier:@"HomeDaoHangCollectionViewCell"];
    [self.daoHangView addSubview:self.collectionview];
    
    [self bringSubviewToFront:self.view];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.backgroundColor = kNightModeColor;
        self.collectionview.backgroundColor = kNightModeColor;
        ;
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.collectionview.backgroundColor = [UIColor whiteColor];
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeDaoHangCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeDaoHangCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.showArray[indexPath.item];
    return cell;
}
- (void)refresh {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.backgroundColor = kNightModeColor;
        self.collectionview.backgroundColor = kNightModeColor;
        ;
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.collectionview.backgroundColor = [UIColor whiteColor];
    }
    [self.collectionview reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.showArray.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.scrollBlock(indexPath.item);
    HomeDaoHangCollectionViewCell *cell = (HomeDaoHangCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    for (int i = 0; i < self.showArray.count; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForItem:i inSection:0];
        HomeDaoHangCollectionViewCell *cell = (HomeDaoHangCollectionViewCell *)[self.collectionview cellForItemAtIndexPath:path];
        cell.bottom.backgroundColor = [UIColor clearColor];
    }
    cell.bottom.backgroundColor = [UIColor redColor];
    self.indexpathItem = indexPath.item;
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeDaoHangCollectionViewCell *cell = (HomeDaoHangCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.bottom.backgroundColor = [UIColor clearColor];
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item != self.indexpathItem) {
        HomeDaoHangCollectionViewCell *cell1 = (HomeDaoHangCollectionViewCell *)cell;
        cell1.bottom.backgroundColor = [UIColor clearColor];
    } else {
        HomeDaoHangCollectionViewCell *cell1 = (HomeDaoHangCollectionViewCell *)cell;
        cell1.bottom.backgroundColor = [UIColor redColor];
    }
}

-(void)getblock:(block)scrollBlock {
    self.scrollBlock = scrollBlock;
}

-(void)setX:(CGFloat)x {
    _x = x;
    CGFloat xx = x * self.collectionview.contentSize.width;
    if (xx < self.collectionview.width / 5 * (self.showArray.count - 5)) {
        [self.collectionview setContentOffset:CGPointMake(xx, 0) animated:NO];
    } else {
        [self.collectionview setContentOffset:CGPointMake(self.collectionview.width / 5 * (self.showArray.count - 5), 0) animated:NO];
    }
    
    if (self.showArray.count < 6) {
        [self.collectionview setContentOffset:CGPointMake(0, 0) animated:NO];
    }
    
}

-(void)setPage:(NSInteger)page {
    _page = page;
    self.indexpathItem = page;
    for (int i = 0; i < self.showArray.count; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForItem:i inSection:0];
        HomeDaoHangCollectionViewCell *cell = (HomeDaoHangCollectionViewCell *)[self.collectionview cellForItemAtIndexPath:path];
        cell.bottom.backgroundColor = [UIColor clearColor];
    }
    
    NSIndexPath *path = [NSIndexPath indexPathForItem:self.page inSection:0];
    HomeDaoHangCollectionViewCell *cell = (HomeDaoHangCollectionViewCell *)[self.collectionview cellForItemAtIndexPath:path];
    cell.bottom.backgroundColor = [UIColor redColor];
}

#pragma mark -- 下拉菜单
-(void)button:(UIButton *)sender {
    if (self.ifClick == NO) {
        self.ifClick = YES;
        [self.view setImage:[[UIImage imageNamed:@"up"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            [UIView setAnimationDelay:0];
            self.height = kHeight - 108;
            self.downView.frame = CGRectMake(0, 0, self.width, kHeight - 108);
            self.downView.alpha = 1;
        }];
    } else if (self.ifClick == YES) {
        [self saveArray];
        self.showArray = self.downView.showArray;
        self.ifClick = NO;
        [self.view setImage:[[UIImage imageNamed:@"down"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            [UIView setAnimationDelay:0];
            self.height = self.heightOld;
            self.downView.frame = CGRectMake(0, 0, self.width, 0);
            self.downView.alpha = 0;
            self.downView.ifDone = NO;
            [self.downView.collection reloadData];
            [self.downView.done setTitle:@"排序或删除" forState:UIControlStateNormal];
            self.downView.chang.text = @"切换频道";
        }];
        self.indexpathItem = 0;
        [self.delegate freshArray];
        self.scrollBlock(0);
        [self.collectionview reloadData];
        
    }
}
#pragma mark -- 数组存本地
-(void)saveArray {
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
    NSString*newFielPath = [documentsPath stringByAppendingPathComponent:@"aa.txt"];
    [self.downView.showArray writeToFile:newFielPath atomically:YES];
}

#pragma mark -- 本地取数组
-(NSArray *)getArray {
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
    NSString *newFielPath = [documentsPath stringByAppendingPathComponent:@"aa.txt"];
    return [NSArray arrayWithContentsOfFile:newFielPath];
}

//完成切换按钮
-(void)done:(UIButton *)sender {
    self.ifClick = NO;
    [self.view setImage:[[UIImage imageNamed:@"down"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.3 animations:^{
        [UIView setAnimationDelay:0];
        self.downView.frame = CGRectMake(0, 0, self.width, 0);
        self.downView.alpha = 0;
    }];
}




@end
